{
  "nbformat": 4,
  "nbformat_minor": 0,
  "metadata": {
    "colab": {
      "name": "clvc-infer-gh.ipynb",
      "provenance": [],
      "collapsed_sections": [],
      "include_colab_link": true
    },
    "kernelspec": {
      "name": "python3",
      "display_name": "Python 3"
    },
    "accelerator": "GPU"
  },
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "view-in-github",
        "colab_type": "text"
      },
      "source": [
        "<a href=\"https://colab.research.google.com/github/Jeevesh8/Cross-Lingual-Voice-Cloning/blob/master/clvc_infer_gh.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "9Y5fGi1KZQul",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "from google.colab import drive\n",
        "drive.mount('/content/drive')"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "vzq7y7s1Q-hU",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "!pip install Unidecode\n",
        "!pip install tensorflow==1.15.2"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "FiliZNEzRTAi",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "%cd '/content/drive/My Drive'"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "7fY3gb4Ed0kK",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "!git clone https://github.com/Jeevesh8/Cross-Lingual-Voice-Cloning Cross-Lingual-Voice-Cloning-gpu"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "nZ5zbhNXd-KR",
        "colab_type": "code",
        "outputId": "0c516b7a-e05e-4c0a-8223-b859cdb95c1d",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 34
        }
      },
      "source": [
        "%cd Cross-Lingual-Voice-Cloning-gpu"
      ],
      "execution_count": 0,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "/content/drive/My Drive/projects/dragoman/Cross-Lingual-Voice-Cloning-gpu-1\n"
          ],
          "name": "stdout"
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "6jHWcoaLeWlX",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "!git submodule init\n",
        "!git submodule update"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "eU5WBlXkRIXV",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "import matplotlib\n",
        "%matplotlib inline\n",
        "import matplotlib.pylab as plt\n",
        "\n",
        "import sys\n",
        "sys.path.append('waveglow/')\n",
        "import numpy as np\n",
        "import torch\n",
        "\n",
        "from hparams import create_hparams\n",
        "from layers import TacotronSTFT, STFT\n",
        "from audio_processing import griffin_lim\n",
        "from train import load_model\n",
        "from text import text_to_sequence"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "UbI--QXa5G2t",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "from model import Tacotron2"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "aFCAEQWeZ_g1",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "base_dir = '/content/Drive/My Drive'"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "wKesiY2zElgN",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "%cd ./waveglow\n",
        "waveglow_path = base_dir+'<waveglow-weights-path>'                              # To get universal vocoder waveglow weights--> https://drive.google.com/open?id=1okuUstGoBe_qZ4qUEF8CcwEugHP7GM_b\n",
        "new_model_path = base_dir+'<waveglow-new-weights-path>'\n",
        "!python convert_model.py '<waveglow-wts-old-path>'  '<waveglow-wts-new-path>'\n",
        "%cd .."
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "GPANKGSSYdkr",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "waveglow = torch.load(new_model_path)['model']\n",
        "waveglow.cuda().eval() #.half()\n",
        "for k in waveglow.convinv:\n",
        "    k.float()\n",
        "#denoiser = Denoiser(waveglow)"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "AtBPhPipRgAU",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "hparams = create_hparams()\n",
        "hparams.sampling_rate = 22050\n",
        "hparams.max_decoder_steps = 1000\n",
        "hparams.n_speakers = 917\n",
        "hparams.dim_yo = 917\n",
        "hparams.lang_embedding_dim = 4\n",
        "#set other hparams you want to change"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "dVm7KT9Sc01t",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "print(hparams.dim_yo)"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "co5wkZgwRgnB",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "checkpoint_path = '<path-to-checkout-of model>'\n",
        "model = load_model(hparams)\n",
        "model.load_state_dict(torch.load(checkpoint_path)['state_dict'])\n",
        "_ = model.cuda().eval() #.half()"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "jJLvwGLgSI5K",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "text = \"Could there be a talking plot\"\n",
        "sequence = np.array(text_to_sequence(text, ['basic_cleaners']))[None, :]\n",
        "sequence = torch.autograd.Variable(\n",
        "    torch.from_numpy(sequence)).cuda().long()"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "nMO9Mb2FThDu",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "print(sequence.shape)"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "c6C3ZpEQSKP4",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "speaker = torch.tensor([13], device='cuda:0')\n",
        "lang = torch.tensor([0], device='cuda:0')\n",
        "mel_outputs, mel_outputs_postnet, _, alignments = model.inference(sequence, speaker, lang)\n",
        "print(mel_outputs.shape)"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "irwl6YvaDsGD",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "import IPython.display as ipd\n",
        "with torch.no_grad():\n",
        "    audio = waveglow.infer(mel_outputs, sigma=0.1)                                    #PUT MEL IN MEL_OUTPUTS_POSTNET\n",
        "print(audio.shape)\n",
        "ipd.Audio(audio[0].data.cpu().numpy(), rate=22050)"
      ],
      "execution_count": 0,
      "outputs": []
    }
  ]
}